package com.proximity.json;

import java.lang.reflect.Type;
import java.util.Collection;

import com.google.gson.reflect.TypeToken;
import com.proximity.json.bean.User;
import com.proximity.json.bean.UserDto;
import com.proximity.json.bean.UserLocation;

public class JsonClient {
	
	private static final String SERVER_JSON = "http://localhost:8080/json";
	private static final String SUFIX_SAVE = "/save";
	private static final String SUFIX_LOCATION = "/location";

	public static void main(String[] args) {

//		UserLocation user = new UserLocation();
//		user.setId(1L);
//		user.setLatitude(2F);
//		user.setLatitudeSpan(4000000);
//		user.setLongitude(1F);
//		user.setLongitudeSpan(4000000);
//		Collection<User> users = updateLocation(user);
//		for (User u : users) {
//			System.out.println("id: " + u.getId() + " long: " + u.getLongitude() + " lat: " + u.getLatitude());
//		}
		
//		UserDto user = new UserDto();
//		user.setLatitude(1F);
//		user.setLongitude(1F);
//		user.setPassword("hhe");
//		user.setRole("ADMIN");
//		user.setUser("ultimo");
//		Long id = saveUser(user);
//		System.out.println(id);
//		
//		UserDto user2 = new UserDto();
//		user2.setLatitude(2F);
//		user2.setLongitude(2F);
//		user2.setPassword("hhe");
//		user2.setRole("ADMIN");
//		user2.setUser("segundo");
//		Long id2 = saveUser(user2);
//		
//		UserDto user3 = new UserDto();
//		user3.setLatitude(3F);
//		user3.setLongitude(3F);
//		user3.setPassword("hhe");
//		user3.setRole("ADMIN");
//		user3.setUser("tercero");
//		Long id3 = saveUser(user3);
//		
//		UserDto user4 = new UserDto();
//		user4.setLatitude(4F);
//		user4.setLongitude(4F);
//		user4.setPassword("hhe");
//		user4.setRole("ADMIN");
//		user4.setUser("cuarto");
//		Long id4 = saveUser(user4);
//		System.out.println(id + ", " + id2 + ", " + id3 + ", " + id4);
	}
	
	public static Collection<User> updateLocation(UserLocation user) {
		
		Collection<User> list = null;
		try {
			Type targetType = new TypeToken<Collection<User>>(){}.getType();
			list = JsonHelper.callMethodJson(SERVER_JSON+SUFIX_LOCATION, user, targetType);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
		
	}
	
	public static Long saveUser(UserDto user) {
		Long id = null;
		try {
			id = JsonHelper.callMethodJson(SERVER_JSON+SUFIX_SAVE, user, Long.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
	
}
